﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace RBAC.APP
{
    public class Function
    {
        #region Fields
        /// <summary>
        /// 功能Id
        /// </summary>
        private string functionId;
        /// <summary>
        /// 功能Id
        /// </summary>
        public string FunctionId
        {
            get { return functionId; }
            //set { functionId = value; }
        }
        /// <summary>
        /// 功能名称
        /// </summary>
        private string functionName;
        /// <summary>
        /// 功能名称
        /// </summary>
        public string FunctionName
        {
            get { return functionName; }
            //set { functionName = value; }
        }
        /// <summary>
        /// 功能描述
        /// </summary>
        private string functionDescription;
        /// <summary>
        /// 功能描述
        /// </summary>
        public string FunctionDescription
        {
            get { return functionDescription; }
            //set { functionDescription = value; }
        }
        #endregion Fields       

        /// <summary>
        /// 获得方法
        /// </summary>
        /// <param name="functionId">方法Id</param>
        public static Function ReadFunction(string functionId)
        {
            //--------------------------------------------------------
            //获得表：SFunctionLib中的一个Function
            //（1）参数：functionId，不可以为空
            //若为空，直接返回null
            //若此Function不存在，则返回null
            //（2）返回：Function对象
            //若存在此Function，则返回Function对象
            //--------------------------------------------------------
            if (string.IsNullOrEmpty(functionId))
                return null;
            RBAC.ORM.DAL.SFunctionLib optSFunctionLib = new RBAC.ORM.DAL.SFunctionLib();
            RBAC.ORM.Model.SFunctionLib mySFunctionLib = new RBAC.ORM.Model.SFunctionLib();
            bool ok = optSFunctionLib.Exists(functionId);
            if (!ok)
                return null;
            else
            {
                mySFunctionLib = optSFunctionLib.GetModel(functionId);
                Function myFunction = new Function();
                myFunction.functionId = mySFunctionLib.funID;
                myFunction.functionName = mySFunctionLib.functionname;
                myFunction.functionDescription = mySFunctionLib.functiondescription;
                return myFunction;
            }
               
        }

        /// <summary>
        /// 阅读方法
        /// </summary>
        /// <param name="howMany">数量</param>
        public static List<Function> ReadFunctions(int? howMany)
        {
            //--------------------------------------------------------
            //获得表：SFunctionLib中一定数量的记录
            //此方法调用本类中的SearchFunctions方法
            //--------------------------------------------------------
            return SearchFunctions(null, howMany);
        }

        /// <summary>
        /// 阅读方法
        /// </summary>
        /// <param name="functionNameorDescription">方法名或方法描述</param>
        /// <param name="howMany">数量</param>
        public static List<Function> SearchFunctions(string functionNameorDescription, int? howMany)
        {
            //--------------------------------------------------------
            //搜索表:SFunctionLib中一定数量的记录，数量为参数howMany。
            //（1）参数：howMany，可为空
            //若为空，则参数值为RBACAPPDefaultValue.howMany_FunctionsToRead值
            //如果读取上述参数异常，howMany默认为100
            //（2）参数：functionNameorDescription，可为空
            //若为空，则搜索所有内容，若不为空，则搜索表：SFunctionLib中functionName或functiondescription字段值中包含此内容的记录
            //（3）返回：泛型Function
            //将获得的记录组织成泛型返回，根据funId正排序。在构建单个Function对象时使用ReadFunction方法
            //若不存在，返回null。
            //--------------------------------------------------------
            if (howMany == null)
            {
                try
                {
                    howMany = RBACAPPDefaultValue.HowMany_FunctionsToRead;
                }
                catch
                {
                    howMany = 100;
                }
            }
            List<Function> myListFunction = new List<Function>();
            RBAC.ORM.DAL.SFunctionLib optSFunctionLib = new RBAC.ORM.DAL.SFunctionLib();
            if (string.IsNullOrEmpty(functionNameorDescription))
            {
                DataSet ds = new DataSet();
                ds = optSFunctionLib.GetList("1=1 order by funID asc");
                if (ds.Tables[0].Rows.Count == 0)
                    return null;
                else
                {
                    if (howMany < ds.Tables[0].Rows.Count)
                    {
                        for (int i = 0; i < howMany; i++)
                        {
                            myListFunction.Add(ReadFunction(ds.Tables[0].Rows[i]["funID"].ToString()));
                        }
                    }
                    else
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            myListFunction.Add(ReadFunction(ds.Tables[0].Rows[i]["funID"].ToString()));
                        }
                    }
                    return myListFunction;
                }
            }
            else
            {
                DataSet ds = new DataSet();
                ds = optSFunctionLib.GetList("functionName like '%" + functionNameorDescription + "%' or functiondescription like '%" + functionNameorDescription + "%' order by funID asc");
                if (ds.Tables[0].Rows.Count == 0)
                    return null;
                else
                {
                    if (howMany < ds.Tables[0].Rows.Count)
                    {
                        for (int i = 0; i < howMany; i++)
                        {
                            myListFunction.Add(ReadFunction(ds.Tables[0].Rows[i]["funID"].ToString()));
                        }
                    }
                    else
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            myListFunction.Add(ReadFunction(ds.Tables[0].Rows[i]["funID"].ToString()));
                        }
                    }
                    return myListFunction;
                }
            }
        }

        /// <summary>
        /// 引用角色
        /// </summary>
        public List<Role> ReferenceRoles
        {
            get
            {
                //--------------------------------------------------------
                //搜索表：SRoleFunction，找出哪些Role包含了此Function
                //（1）参数：this.FunctionId，当前对象的functionId
                //若为空，则返回null。
                //若不为空，则搜索表：SRoleFunction中对应的roleId，根据roleId正排序
                //对roleId列表分别构造其相应的Role对象，并形成泛型Role
                //若没有对应的roleId，则返回null
                //（2）返回：泛型Role
                //--------------------------------------------------------
                RBAC.ORM.DAL.SRoleFunction optSRoleFunction = new RBAC.ORM.DAL.SRoleFunction();
                List<Role> myListRole = new List<Role>();
                if (string.IsNullOrEmpty(this.FunctionId))
                    return null;
                else
                {
                    DataSet ds = new DataSet();
                    ds = optSRoleFunction.GetList("funID='" + this.FunctionId + "' order by roleID asc");
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        myListRole.Add(Role.ReadRole(ds.Tables[0].Rows[i]["roleID"].ToString()));
                    }
                    return myListRole;
                }
            }
        }
       
    }
}
